home *** CD-ROM | disk | FTP | other *** search
/ Aminet 38 / Aminet 38 (2000)(Schatztruhe)[!][Aug 2000].iso / Aminet / util / misc / MultiRen.lha / MultiRen / Developers / SwapName-Plugin.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-03  |  5.3 KB  |  186 lines

  1. /* Swapname-plugin v1.0 by Deniil 715! for MultiRen
  2.    Made 2000-04-15
  3.  
  4.    My e-mail: Daniel Westerberg: deniil@algonet.se
  5. */
  6.  
  7. #include<proto/exec.h>
  8. #include<proto/dos.h>
  9. #include<proto/intuition.h>
  10. #include<exec/tasks.h>
  11. #include<intuition/intuition.h>
  12. #include<stdlib.h>
  13. #include<stdio.h>
  14. #include<string.h>
  15.  
  16. #define _VER "$VER: Template v1.0 by Deniil 715! rev.#1 (2000-04-15)"
  17. #define NUMSTRINGS_FOR_THIS_PLUGIN 1
  18. #define FILE_NAME_LENGTH 512
  19. #define COM_ASK 1
  20. #define COM_EXTRACT 2
  21. #define COM_CONFIGURE 3
  22. #define COM_ABOUT 4
  23. #define COM_QUIT 5
  24. #define ERR_OK 0
  25. #define ERR_NOMEM 1
  26. #define ERR_NOFILE 2
  27. #define ERR_NOSIG 3
  28. #define ERR_NOTIMPL 4
  29. #define ERR_UNKNOWN 5
  30. #define ERR_OTHER 6
  31. #define ERR_WRONGFORMAT 7
  32. #define ERR_FATAL 20
  33.  
  34. struct multiren_plugin {
  35.   long id;
  36.   Task *task;
  37.   long sig;
  38.   short ret;
  39.   char command;
  40.   char numstrings;       /* You will */
  41.   char *stringlist[256]; /* only use */
  42.   char *name;            /* these 3 elements */
  43. };
  44.  
  45. short ask(void);
  46. short extract(void);
  47. short configure(void);
  48. short about(void);
  49. short cleanup(void);
  50. short req(char*,char*);
  51.  
  52. struct multiren_plugin *mrp;
  53. char  s[FILE_NAME_LENGTH+1];
  54. short inclext=1;
  55. BPTR  fh;
  56.  
  57. short main(int argc,char *argv[]) {
  58.   char sigbit;
  59.   long sig, msig;
  60.   Task *mtask;
  61.   if(DOSBase=(struct DosLibrary*)OpenLibrary("dos.library",0)) {
  62.     if(IntuitionBase=(struct IntuitionBase*)OpenLibrary("intuition.library",0)) {
  63.       if(argc==2) {
  64.         if(mrp=(struct multiren_plugin*)atol(argv[1])) {
  65.           if(mrp->id==*(long*)"MRPO") {
  66.             if((sigbit=AllocSignal(-1))>=0) {
  67.               sig=1<<sigbit;
  68.               while(1) {
  69.                 switch(mrp->command) {
  70.                 case COM_ASK:
  71.                   mtask=mrp->task;
  72.                   msig=mrp->sig;
  73.                   mrp->ret=ask();
  74.                   SetProgramName(mrp->name);
  75.                   mrp->task=FindTask(0L);
  76.                   mrp->sig=sig;
  77.                   break;
  78.                 case COM_EXTRACT:   mrp->ret=extract(); break;
  79.                 case COM_CONFIGURE: mrp->ret=configure(); break;
  80.                 case COM_ABOUT:     mrp->ret=about(); break;
  81.                 case COM_QUIT:
  82.                   mrp->ret=cleanup();
  83.                   FreeSignal(sigbit);
  84.                   CloseLibrary((struct Library*)DOSBase);
  85.                   Signal(mtask,msig);
  86.                   return 0;
  87.                 default:
  88.                   mrp->ret=ERR_UNKNOWN;
  89.                 }
  90.                 Signal(mtask,msig);
  91.                 Wait(sig);
  92.               }
  93.             }
  94.             else {
  95.               mrp->ret=ERR_NOSIG;
  96.               Signal(mrp->task,mrp->sig);
  97.               return 0;
  98.             }
  99.           }
  100.         }
  101.       }
  102.       CloseLibrary((struct Library*)IntuitionBase);
  103.     }
  104.     CloseLibrary((struct Library*)DOSBase);
  105.   }
  106.   printf("This is a plugin for MultiRen!\n"
  107.          "It is not supposed to be executed manually!\n"
  108.          "Use it through the Renplacer tool in MultiRen instead!\n");
  109.   return ERR_FATAL;
  110. }
  111.  
  112. short ask() {
  113.   mrp->numstrings=NUMSTRINGS_FOR_THIS_PLUGIN;   /* I will return one string */
  114.   mrp->stringlist[0]="Filename turned backwards"; /* Setting information-strings */
  115.   mrp->name="SwapName";                         /* This plugins name */
  116.   if(fh=Open("ENVARC:SwapName-plugin.cfg",MODE_OLDFILE)) { /* Open configfile */
  117.     inclext=FGetC(fh); /* The prefs telling if we should include
  118.                           extension in the swap or not.. */
  119.     Close(fh);
  120.   }
  121.   return ERR_OK;
  122. }
  123.  
  124. short extract() { /* Do my stuff.. */
  125.   int  x, y, z;
  126.   char *st;
  127.   st=FilePart(mrp->name); /* Note that we get the complete path in name
  128.                              and must separate it to get the filename. */
  129.   x=strlen(st);
  130.   if(x<FILE_NAME_LENGTH) {
  131.     if(inclext) {
  132.       for(y=0;y<x;y++) s[y]=st[x-1-y];
  133.       s[x]='\0';
  134.     }
  135.     else {
  136.       for(z=x;z>=0;z--) if(st[z]=='.') break;
  137.       if(z>0) z--; else z=x;
  138.       for(y=0;y<=z;y++) s[y]=st[z-y];
  139.       s[z+1]='\0';
  140.       if(z<x) strcat(s,st+z+1);
  141.     }
  142.     mrp->stringlist[0]=s; /* Setting the string-list position 0 to point to our string */
  143.     return ERR_OK;
  144.   }
  145.   else
  146.     return ERR_OTHER; /* Filename was longer than the set max-length for MultiRen v1.3 */
  147. }
  148.  
  149. short configure() {
  150.   char *str;
  151.   if(inclext) str="Yes"; else str="No";
  152.   sprintf(s,"Do you want the extension to be\n"
  153.             "part of the filename swapping??\n"
  154.             "Current state: %s",str);
  155.   inclext=req(s,"Yes|No|Cancel");
  156.   if(inclext) {
  157.     if(fh=Open("ENVARC:SwapName-plugin.cfg",MODE_NEWFILE)) {
  158.       FPutC(fh,inclext & 1);
  159.       Close(fh);
  160.     }
  161.     else
  162.       req("Could not save the settings!","OK");
  163.   }
  164.   return ERR_OK; /* Will always return ERR_OK here so that MultiRen will not
  165.                     put up another requester telling this operation failed. */
  166. }
  167.  
  168. short about() {
  169.   if(req("SwapName Plugin v1.0 by Deniil 715! for MultiRen\n\n"
  170.          "It was made 2000-04-15 in Amiga-E\n\n"
  171.          "E-mail: deniil@algonet.se","More|OK"))
  172.     req("This plugin will swap the filename backwards.\n"
  173.         "It features a setting to include the file-\n"
  174.         "extension in the swapping or not.","OK");
  175.   return ERR_OK;
  176. }
  177.  
  178. short cleanup() { return ERR_OK; }
  179.  
  180. short req(char *body,char *gads) {
  181.   struct EasyStruct tags;
  182.   tags.es_Title="Test Plugin";
  183.   tags.es_TextFormat=body;
  184.   tags.es_GadgetFormat=gads;
  185.   return EasyRequestArgs(NULL,&tags,NULL,NULL);
  186. }